/**
 * 
 */
package cn.mxz.robots.province;

import java.util.Scanner;

import net.sf.json.JSONObject;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import cn.mxz.crawler_single.basic.Request;
import cn.mxz.robots.basic.local.Exam;
import cn.mxz.robots.pojo.Student;

/**
 * 甘肃省
 * 排名：未完成
 * 
 * @author cuizhennan<cuizhennan@mouge.wang>
 * @since 2015-06-22
 */
public class Gansu extends Exam
{
	public static void main(String[] args)
	{
		Student student = new Student("15622826152803", "622826199305271916", "622826199305271916", "15622826152803","");
		Gansu gansu = new Gansu(student);
		System.out.println(gansu.getCaptcha());
		System.out.println("请输入验证码：");
		@SuppressWarnings("resource")
		String captcha = new Scanner(System.in).next();
		System.out.println(gansu.login(captcha));
	}

	public Gansu(Student student)
	{
		super(student);
	}

	@Override
	public String getCaptcha()
	{
		delCookie();
		Request captchaRequest = new Request("http://gaokao.gszs.cn/validatecode.do", mSha1);
		return curlFile(captchaRequest);
	}

	@Override
	public String login(String captcha)
	{
		Request loginRequest = new Request("http://gaokao.gszs.cn/login.do", mSha1);
		loginRequest.setCharSet("utf-8");
		loginRequest.addHeader("Referer", "http://gaokao.gszs.cn/");
		loginRequest.addParams("type", "gkcjcx");
		loginRequest.addParams("userName", mStudent.getStudentId());
		loginRequest.addParams("userPasswd", mStudent.getIdCard());
		loginRequest.addParams("idCode", captcha);
		String loginHtml = curlPost(loginRequest);
		JSONObject resultObject = new JSONObject();

		if (loginHtml.contains("姓  名"))
		{
			resultObject.put("result", LOGIN_SUCCESS);
			resultObject.put("data", loginParseHtml(loginHtml));
		}
		else if (loginHtml.contains("考生不存在"))
		{
			resultObject.put("result", ERROR);
			resultObject.put("data", NOT_EXIST_USER);
		}
		else if (loginHtml.contains("验证码输入错误"))
		{
			resultObject.put("result", ERROR);
			resultObject.put("data", WRONG_CAPTCHA);
		}
		else
		{
			resultObject.put("result", ERROR);
			resultObject.put("data", INTERNAL_SERVER_ERROR);
		}

		return resultObject.toString();
	}


	@Override
	public String loginParseHtml(String parseHtml)
	{
		Document loginDocument = Jsoup.parse(parseHtml);
		JSONObject result = new JSONObject();
		Elements trsif = loginDocument.select("table").get(2).select("tr");
		result.put("exam_id", mStudent.getStudentId());
		result.put("name", trsif.get(0).select("td").get(3).text());
		result.put("ranking", "");
		result.put("province", "甘肃省");
		Elements trssc = loginDocument.select("table").get(3).select("tr");
		JSONObject scoreObject = new JSONObject();
		scoreObject.put("语文", trssc.get(1).select("td").get(0).text());
		scoreObject.put("数学", trssc.get(1).select("td").get(1).text());
		scoreObject.put("综合", trssc.get(1).select("td").get(2).text());
		scoreObject.put("外语", trssc.get(1).select("td").get(3).text());
		scoreObject.put("总分", trsif.get(1).select("td").get(3).text());

		result.put("score", scoreObject.toString());
		return result.toString();
	}
}
